<?php
require_once( 'mofachi.BabyData.php' );
class Baby {
	private $babyData;
	private $is_loaded;
	public function __get( $key ) {
		return $this->babyData->$key;
	}
	public function __set( $key, $value ) {
		switch( $key ) {
			case "baby_id":
				if( !$this->is_loaded ) {
					$this->babyData->baby_id = $value;
					break;
				}
			case "reg_time":
			case "edit_time":
				echo "--cannot set ".$key;
				break;
			default:
				$this->babyData->$key = $value;
		}
	}
	public function __construct() {
		$this->babyData = new BabyData();
		$this->is_loaded = false;
	}
	public function isLoaded() {
		return $this->is_loaded;
	}
	public function add() {
		if( !$this->nickname ) {
			return false;
		}	
		if ( !get_magic_quotes_gpc() ) {
			$this->nickname = addslashes( $this->nickname );
			$this->realname = addslashes( $this->realname );
			$this->gender = addslashes( $this->gender );
			$this->location_id = addslashes( $this->location_id );
			$this->birthday = addslashes( $this->birthday );
		}
		return $this->babyData->add();
	}
	public function loadByID() {
		if( !$this->baby_id ) {
			return false;
		}		
		$this->is_loaded = $this->babyData->loadByID();
		return $this->is_loaded;
	}
	public function loadByNickname() {
		if( !$this->nickname ) {
			return false;
		}		
		$this->is_loaded = $this->babyData->loadByNickname();
		return $this->is_loaded;
	}
	public function update() {
		if( !$this->nickname ) {
			return false;
		}
		if( !get_magic_quotes_gpc() ) {
			$this->nickname = addslashes( $this->nickname );
			$this->realname = addslashes( $this->realname );
			$this->gender = addslashes( $this->gender );
			$this->location_id = addslashes( $this->location_id );
			$this->birthday = addslashes( $this->birthday );
		}
		return $this->babyData->update();
	}
	public function remove() {
		if( !$this->baby_id ) {
			return false;
		}
		$result = $this->babyData->remove();
		if( $result )
			unset( $this->babyData );
		return $result;
	}
	//-------------------------------------------------------------------------------------
	// static functions
	//-------------------------------------------------------------------------------------
	public static function listBaby( $offset, $length ) {
		$offset = ( isset( $offset ) ) ? $offset : 0;
		$length = ( isset( $length ) ) ? $length : 20;

		$babyData = BabyData::listData( $offset, $length );
		$returnBabies = array();
		foreach( $babyData as $bd ) {
			$b = new Baby();
			$b->babyData = $bd;
			array_push( $returnBabies, $b );
		}
		return $returnBabies;
	}
}
?>